/**
 * 修复后的SKU库存检查逻辑测试
 */

// 模拟SKU数据
const mockSkus = [
    {
        id: 1,
        specs: "红色@大码",
        price: 99.00,
        stock: 10
    },
    {
        id: 2,
        specs: "红色@小码",
        price: 99.00,
        stock: 0
    },
    {
        id: 3,
        specs: "蓝色@大码",
        price: 99.00,
        stock: 5
    },
    {
        id: 4,
        specs: "蓝色@小码",
        price: 99.00,
        stock: 8
    }
];

// 构建SKU库存映射表
function buildSkuStockMap(allSkus) {
    const skuStockMap = {};
    
    allSkus.forEach(sku => {
        if (sku.specs) {
            skuStockMap[sku.specs] = {
                sku: sku,
                stock: sku.stock
            };
        }
    });
    
    return skuStockMap;
}

// 修复后的库存状态检查方法
function checkSpecValueStockStatusWithSelection(specValue, currentSpec, selectedSpecs, skuStockMap) {
    let hasStock = false;
    let minStock = Infinity;
    
    // 构建包含当前规格值的临时规格组合
    const tempSpecs = [...selectedSpecs];
    const existingIndex = tempSpecs.findIndex(s => s.name === currentSpec.name);
    
    if (existingIndex !== -1) {
        tempSpecs[existingIndex].v = specValue.value;
    } else {
        tempSpecs.push({ name: currentSpec.name, v: specValue.value });
    }
    
    // 检查该组合是否有库存
    const specsString = tempSpecs.map(spec => spec.v).join('@');
    const skuInfo = skuStockMap[specsString];
    
    console.log(`检查规格组合 "${specsString}" 的库存状态:`, skuInfo);
    
    if (skuInfo && skuInfo.stock > 0) {
        hasStock = true;
        minStock = skuInfo.stock;
        console.log(`规格组合 "${specsString}" 有库存: ${skuInfo.stock}`);
    }
    
    // 如果当前组合没有库存，检查该规格值是否参与其他有库存的组合
    if (!hasStock) {
        console.log(`规格组合 "${specsString}" 无库存，检查规格值 "${specValue.value}" 是否参与其他有库存的组合`);
        
        // 检查该规格值是否与已选择的规格形成任何有效组合
        let canFormValidCombination = false;
        
        Object.keys(skuStockMap).forEach(specsString => {
            const specValues = specsString.split('@');
            if (specValues.includes(specValue.value)) {
                // 检查这个组合是否与已选择的规格兼容
                const isCompatible = selectedSpecs.every(selected => {
                    return specValues.includes(selected.v);
                });
                
                if (isCompatible) {
                    const otherSkuInfo = skuStockMap[specsString];
                    if (otherSkuInfo && otherSkuInfo.stock > 0) {
                        canFormValidCombination = true;
                        hasStock = true;
                        if (otherSkuInfo.stock < minStock) {
                            minStock = otherSkuInfo.stock;
                        }
                        console.log(`规格值 "${specValue.value}" 可以与已选择规格形成有效组合 "${specsString}"，库存: ${otherSkuInfo.stock}`);
                    }
                }
            }
        });
        
        // 如果无法形成有效组合，则禁用
        if (!canFormValidCombination) {
            console.log(`规格值 "${specValue.value}" 无法与已选择的规格形成有效组合，将被禁用`);
        }
    }
    
    const result = {
        status: hasStock ? 'normal' : 'outOfStock',
        disabled: !hasStock,
        stockInfo: { minStock: minStock === Infinity ? 0 : minStock }
    };
    
    console.log(`规格值 "${specValue.value}" 的库存状态:`, result);
    
    return result;
}

// 测试执行
console.log('=== 修复后的SKU库存检查测试 ===');

console.log('1. 构建SKU库存映射表:');
const skuStockMap = buildSkuStockMap(mockSkus);
console.log(skuStockMap);

console.log('\n2. 测试场景：已选择"红色"，检查"大码"的库存状态');
const selectedSpecs1 = [{ name: "颜色", v: "红色" }];
const currentSpec1 = { name: "尺码" };
const specValue1 = { value: "大码" };

const result1 = checkSpecValueStockStatusWithSelection(specValue1, currentSpec1, selectedSpecs1, skuStockMap);
console.log('\n最终结果:', result1);

console.log('\n3. 测试场景：已选择"红色"，检查"小码"的库存状态');
const specValue2 = { value: "小码" };
const result2 = checkSpecValueStockStatusWithSelection(specValue2, currentSpec1, selectedSpecs1, skuStockMap);
console.log('\n最终结果:', result2);

console.log('\n4. 测试场景：已选择"红色"，检查"蓝色"的库存状态');
const currentSpec2 = { name: "颜色" };
const specValue3 = { value: "蓝色" };
const result3 = checkSpecValueStockStatusWithSelection(specValue3, currentSpec2, selectedSpecs1, skuStockMap);
console.log('\n最终结果:', result3);

console.log('\n=== 测试完成 ===');
